using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Trippy.Animation;
using Trippy.Abilities;
using Trippy.CollidableGameComponents;
using Trippy.CollidableGameComponents.Objects;
using Trippy.Levels;

namespace Trippy.CollidableGameComponents.Agents
{
    public enum SwoopEnemyState { None, Grounded }

    public class SwoopingEnemy : Agent
    {

        public static Texture2D SpriteSheet { get; private set; }

        protected override void LoadContent()
        {
            if (SpriteSheet == null) SpriteSheet = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Agents/bird_sheet");
            Sprite = new AnimatedSprite(this, SpriteSheet, 96, 96, 0, 0, -30, 0, 3, 0.1f);
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            StandardDraw(gameTime);
            base.Draw(gameTime);
        }

        public SwoopEnemyState State { get; protected set; }
        public Ability CurrentAbility { get; protected set; }


        public SwoopingEnemy(Level level)
            : base(level)
        {
            State = SwoopEnemyState.None;
            CurrentAbility = null;
        }

        float KnockDown = 0.0f;

        public override void Update(GameTime gameTime)
        {
            float elapsedTime = TrippyUtilities.CalculateElapsedTime(gameTime);

            Vector2 swoop = CurrentLevel.Hero.Position - Position;

            if (swoop.Length() < 700)
            {
                if (!(float.IsNaN(swoop.X) || float.IsNaN(swoop.Y)))
                {
                    Velocity = swoop * elapsedTime * 50f;
                }
            }

            if (KnockDown > 0.0f)
            {
                Velocity += new Vector2(0f, 200f);
                KnockDown -= elapsedTime;
            }

            if (swoop.X < 0)
            {
                Sprite.ChangeAnimation(1);
                Sprite.Step();
            }
            if (swoop.X > 0)
            {
                Sprite.ChangeAnimation(2);
                Sprite.Step();
            }
            if (swoop.X == 0 && swoop.Y > 0)
            {
                Sprite.ChangeAnimation(0);
                Sprite.Step();
            }
            if (swoop.X == 0 && swoop.Y < 0)
            {
                Sprite.ChangeAnimation(3);
                Sprite.Step();
            }

            if (CurrentLevel.Hero.State == Hero.HeroState.Dying)
            {
                Velocity = new Vector2(0, 0);
            }

            UpdateStandardPhysics(elapsedTime);

            base.Update(gameTime);
        }


        protected override void CollisionHandler(CollidableGameComponent component, CollidableGameComponent.CollisionSide side)
        {
            if (component is Hero)
            {
                Hero h = (Hero)component;
                switch (side)
                {
                    case CollisionSide.Top:
                        h.Bounce(BoundingBox.Top);
                        KnockDown = 2.0f;
                        break;
                    case CollisionSide.Right:
                        h.HitRightWall(BoundingBox.Right);
                        break;
                    case CollisionSide.Bottom:
                        h.Die(Hero.MannerOfDeath.Enemy);
                        break;
                    case CollisionSide.Left:
                        h.HitLeftWall(BoundingBox.Left);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("side");
                }
            }

            base.CollisionHandler(component, side);
        }


        public override void Restore()
        {
            KnockDown = 0f;
            Sprite.Tint = Color.White;
            base.Restore();
        }

    }
}
